﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//using System.Web.WebPages.Html;
using System.Web.Mvc;
using System.Web.Routing;

namespace EasyUIDemo1.Helpers
{
    public static class HtmlHeplerExtensions
    {
        // 我对ASP.NET MVC HtmlHelper中的Form和Link的小扩展
        public static string DatePicker(this HtmlHelper htmlHelper, string name, string value)
        {

            return "<script type=\"text/javascript\">" +
                     "$(function() {" +
                     "$(\"#" + name + "\").datepicker();" +
                     "});" +
                     "</script>" +
                     "<input type=\"text\" size=\"10\" value=\"" + value + "\" id=\"" + name + "\" name=\"" + name + "\"/>";

        }

        public static MvcHtmlString ActionLinkForJS(this HtmlHelper htmlHelper, string linkText, IDictionary<string, object> htmlAttributes)
        {
            if (String.IsNullOrEmpty(linkText))
            {
                throw new ArgumentException("linkText");
            }

            TagBuilder linkTag = new TagBuilder("a");
            linkTag.InnerHtml = linkText;
            linkTag.MergeAttribute("href", "javascript:void(0);");
            linkTag.MergeAttributes(htmlAttributes);

            
            return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.Normal));
        }

        // <img src="images/61.png" alt="" />

        public static MvcHtmlString ActionLinkForJS(this HtmlHelper htmlHelper, string linkText, object htmlAttributes)
        {
            return ActionLinkForJS(htmlHelper, linkText, new RouteValueDictionary(htmlAttributes));
        }

        public static MvcHtmlString ActionLinkForJS(this HtmlHelper htmlHelper, string linkText, IDictionary<string, object> htmlAttributes, IDictionary<string, object> imgAttributes)
        {
            if (String.IsNullOrEmpty(linkText))
            {
                throw new ArgumentException("linkText");
            }

            TagBuilder linkTag = new TagBuilder("a");
            //linkTag.InnerHtml = linkText;
            linkTag.MergeAttribute("href", "javascript:void(0);");
            linkTag.MergeAttributes(htmlAttributes);

            TagBuilder imgTag = new TagBuilder("img");
            imgTag.MergeAttributes(imgAttributes);
            linkTag.InnerHtml = imgTag.ToString(TagRenderMode.Normal) + linkText;

            return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ActionLinkForJS(this HtmlHelper htmlHelper, string linkText, object htmlAttributes, object imgAttributes)
        {
            return ActionLinkForJS(htmlHelper, linkText, new RouteValueDictionary(htmlAttributes), new RouteValueDictionary(imgAttributes));
        }
    }
}